package com.harveyguo.nihonngo.controller;

import com.harveyguo.nihonngo.model.dto.BoxingGymDto;
import com.harveyguo.nihonngo.service.BoxingGymService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 拳馆Controller
 */
@RestController
@RequestMapping("/api/gyms")
public class BoxingGymController {

    @Autowired
    private BoxingGymService boxingGymService;

    /**
     * 创建拳馆
     */
    @PostMapping
    public ResponseEntity<BoxingGymDto> createGym(@RequestBody BoxingGymDto gymDto) {
        return ResponseEntity.ok(boxingGymService.createGym(gymDto));
    }

    /**
     * 根据ID查找拳馆
     */
    @GetMapping("/{id}")
    public ResponseEntity<BoxingGymDto> getGym(@PathVariable String id) {
        return ResponseEntity.ok(boxingGymService.findById(id));
    }

    /**
     * 查找所有拳馆
     */
    @GetMapping
    public ResponseEntity<List<BoxingGymDto>> getAllGyms() {
        return ResponseEntity.ok(boxingGymService.findAll());
    }

    /**
     * 根据认证状态查找拳馆
     */
    @GetMapping("/verified/{verified}")
    public ResponseEntity<List<BoxingGymDto>> getGymsByVerified(@PathVariable Boolean verified) {
        return ResponseEntity.ok(boxingGymService.findByVerified(verified));
    }

    /**
     * 根据名称查找拳馆
     */
    @GetMapping("/name/{name}")
    public ResponseEntity<List<BoxingGymDto>> getGymsByName(@PathVariable String name) {
        return ResponseEntity.ok(boxingGymService.findByName(name));
    }

    /**
     * 根据地址查找拳馆
     */
    @GetMapping("/address/{address}")
    public ResponseEntity<List<BoxingGymDto>> getGymsByAddress(@PathVariable String address) {
        return ResponseEntity.ok(boxingGymService.findByAddress(address));
    }

    /**
     * 查找附近的拳馆
     */
    @GetMapping("/nearby")
    public ResponseEntity<List<BoxingGymDto>> getNearbyGyms(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam(defaultValue = "10.0") Double radius) {
        return ResponseEntity.ok(boxingGymService.findNearbyGyms(latitude, longitude, radius));
    }

    /**
     * 更新拳馆信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<BoxingGymDto> updateGym(@PathVariable String id, @RequestBody BoxingGymDto gymDto) {
        return ResponseEntity.ok(boxingGymService.updateGym(id, gymDto));
    }

    /**
     * 删除拳馆
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGym(@PathVariable String id) {
        boxingGymService.deleteGym(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 认证拳馆
     */
    @PostMapping("/{id}/verify")
    public ResponseEntity<BoxingGymDto> verifyGym(@PathVariable String id) {
        return ResponseEntity.ok(boxingGymService.verifyGym(id));
    }
}